<template>
  <el-dialog
    :title="dialogTitle"
    :modal-append-to-body='false'
    :visible.sync="visible">
    <el-form :model="dataForm"  :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <el-row >
        <el-col :span="12">
          <el-form-item label="供应商名称">
            <el-input v-model="parentForm.orgName" placeholder="代理商名称" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="许可证编号" prop="perNo">
            <el-input v-model="dataForm.perNo" placeholder="许可证编号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="经营方式" prop="operateType">
            <dictionary-select dictType="operate_type" :dictionaryCode.sync="dataForm.operateType" :dictionaryName.sync="dataForm.operateTypeName"></dictionary-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="法人" prop="legalName">
            <el-input v-model="dataForm.legalName" placeholder="请输入法人"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="负责人" prop="principalName">
            <el-input v-model="dataForm.principalName" placeholder="请输入负责人"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="住所" prop="perAddr">
            <el-input v-model="dataForm.perAddr" placeholder="请输入负责人住所"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="经营场所" prop="businessAddr">
            <el-input v-model="dataForm.businessAddr" placeholder="请输入经营场所"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="库房地址" prop="regOrg">
            <el-input v-model="dataForm.warehouseAddr" placeholder="请输入库房地址"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开始日期" prop="beginDate">
            <el-date-picker
              v-model="dataForm.beginDate"
              type="datetime"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结束日期" prop="endDate">
            <el-date-picker
              :disabled="dataForm.flag == 1"
              v-model="dataForm.endDate"
              type="datetime"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              placeholder="选择日期时间">
            </el-date-picker>
            <el-switch
              v-model="dataForm.flag"
              active-text="永久"
              active-value="1"
              inactive-value="0"
              @change="flagChange"
            >
            </el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="发证机关" prop="regOrg">
            <el-input v-model="dataForm.regOrg" placeholder="请输入发证机关"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="发证日期" prop="regDate">
            <el-date-picker
              v-model="dataForm.regDate"
              type="datetime"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="经营范围" prop="catIdList">
            <div  @click="showMedCat">
              <el-input
                :placeholder="dataForm.catIdList ? '点击查看经营范围分类' : '请选择经营范围分类'"
                disabled
              >
                <i slot="suffix" class="iconfont" style="font-size: 28px" >&#xe629;</i>
              </el-input>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注信息" prop="remarks">
            <el-input v-model="dataForm.remarks" placeholder="请输入备注信息" type="textarea"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="图片上传" prop="pic">
            <el-input v-model="dataForm.pic" placeholder="请上传图片"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div v-if="isShowHistory">
      <el-table
        border
        :data="historyList"
        max-height="200"
        style="width: 100%">
        <el-table-column
          label="证号"
          width="180">
          <template slot-scope="scope">
            <a style="color: #00a0e9;cursor: pointer" @click="checkLic(scope.row)">{{ scope.row.perNo }}</a>
          </template>
        </el-table-column>
        <el-table-column
          prop="beginDate"
          label="有效期开始日期"
          width="180">
        </el-table-column>
        <el-table-column
          label="有效期截止日期"
          width="180">
          <template slot-scope="scope">
            {{ scope.row.endDate ? scope.row.endDate : '永久' }}
          </template>
        </el-table-column>
      </el-table>
    </div>
    <span slot="footer" class="dialog-footer">
       <el-button class="fl" @click="showHistory">查看历史证件</el-button>
      <el-button @click="changeCertificate" type="info" v-if="flag">换证</el-button>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    <med-cat-dialog v-if="medDialogVisible"  ref="medCatDialog" :catIdList.sync="dataForm.catIdList" :devicesVersionFlag.sync="dataForm.devicesVersionFlag"></med-cat-dialog>
  </el-dialog>
</template>
<script>
  import dictionarySelect from '@/components/common/dictionary-select'
  import medCatDialog from '@/components/basedata/organinfo/med-cat-dialog'
  const defaultForm = {
    flag: 0,
    beginDate: '',
    catIdList: [],
    legalName: '',
    operateType: '',
    operateTypeName: '',
    orgCode: '',
    perAddr: '',
    perNo: '',
    principalName: '',
    regDate: '',
    regOrg: '',
    remarks: '',
    warehouseAddr: '',
    devicesVersionFlag: 0
  }
  export default {
    name: 'hospitalinfo-add-or-update',
    data () {
      return {
        medDialogVisible: '',
        visible: false,
        dataForm: {},
        parentForm: {},
        isAdd: false,
        dataRule: {
          orgName: [
            { required: true, message: '承运商名称不能为空', trigger: 'blur' }
          ],
          perNo: [
            { required: true, message: '登记号不能为空', trigger: 'blur' }
          ],
          ecoType: [
            { required: true, message: '经济性质不能为空', trigger: 'blur' }
          ]
        },
        // 弹窗标题
        dialogTitle: '',
        isShowHistory: false,
        // 历史证件列表
        historyList: [],
        flag: false
      }
    },
    components: {
      dictionarySelect,
      medCatDialog
    },
    methods: {
      // 选择经营范围
      showMedCat () {
        this.medDialogVisible = true
        this.$nextTick(() => {
          this.$refs.medCatDialog.init()
        })
      },
      // 切换历史证件
      checkLic (row) {
        this.isShowHistory = false
        this.getLicDetail(row)
      },
      // 获取证件详情
      getLicDetail (row) {
        this.isAdd = false
        this.$http({
          url: this.$http.adornUrl(`${this.$API.BASE_INFO.ORGAN_PER.SPL_INFO}/${row.perCode}`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.dataForm = data
        })
      },
      // 查看历史证件列表
      showHistory () {
        this.isShowHistory = !this.isShowHistory
        if (this.isShowHistory) {
          this.$http({
            url: this.$http.adornUrl(`${this.$API.BASE_INFO.ORGAN_PER.SPL_RESTORY}/${this.parentForm.orgCode}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            this.historyList = data
          })
        }
      },
      // 换证
      changeCertificate () {
        if (this.dataForm.perCode) {
          const data = {
            parentId: this.dataForm.perCode
          }
          this.dataForm = Object.assign(data, defaultForm)
        }
        this.dialogTitle = '换证'
        this.isAdd = true
      },
      // 永久日期切换项
      flagChange (data) {
        if (data === '0' || data === '1') {
          const flag = 0
          data = flag ? this.dataForm.endDate : this.dataForm.endDate = ''
        } else {
          this.dataForm.flag = 0
        }
      },
      // 页面初始化
      init (row) {
        this.visible = true
        this.isShowHistory = false
        this.$nextTick(() => {
          this.dataForm = Object.assign(defaultForm)
          this.parentForm = row
          this.$refs['dataForm'].resetFields()
          if (row.orgCode) {
            this.$http({
              url: this.$http.adornUrl(`${this.$API.BASE_INFO.ORGAN_PER.SPL_LIST}/${row.orgCode}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data) {
                this.isAdd = false
                this.dialogTitle = '供应商经营许可证编辑'
                this.dataForm = data
                this.flag = true
              } else {
                this.dialogTitle = '供应商经营许可证新增'
                this.isAdd = true
                this.flag = false
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.dataForm.orgCode = this.parentForm.orgCode
            this.$http({
              url: this.$http.adornUrl(`${this.isAdd ? this.$API.BASE_INFO.ORGAN_PER.SPL_SAVE : this.$API.BASE_INFO.ORGAN_PER.SPL_UPDATE}`),
              method: 'post',
              data: this.$http.adornData(this.dataForm)
            }).then(() => {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                }
              })
            })
          }
        })
      }
    }
  }
</script>
